<?php
class CateProject_Model {
	public $con;
	public function __construct(){
		$this->con =  mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME);
		if(mysqli_connect_error())
		{
			echo "Failed to connect to MySQL". mysqli_connect_error();exit();
		}	
	}
	public function getListCate() {
		$query = "SELECT c.id,c.category_name,c.category_description FROM project_categories AS c";
		$result = mysqli_query($this->con,$query);
		return $result;
	}
	public function getListProjectforEachcat($id) {
		$query = "SELECT p.id, p.title, p.image, p.content
		FROM projects AS p, project_categories AS cp, project_category_midle AS pc
		WHERE p.id = pc.idproject
		AND pc.idcategory = cp.id
		AND cp.id ='$id'";
		$result = mysqli_query($this->con,$query);
		return $result;
	}
	public function viewCat($id)
	{
		$query = "SELECT * FROM project_categories WHERE id='$id'";
		$result = mysqli_query($this->con,$query);
		return $result;
	}
	public function addCat($nameCat,$description) {
		$query = "INSERT INTO project_categories(category_name,category_description) VALUE ('$nameCat','$description')";
		$result = mysqli_query($this->con,$query);
	}

	public function editCat($nameCat,$description,$id) {
		$query = "UPDATE project_categories SET category_name = '$nameCat',category_description = '$description' WHERE id = '$id'";
		$result = mysqli_query($this->con,$query);
	}

	public function beforeditCat($id) {
		$query = "SELECT * FROM project_categories WHERE id=".$id;
		$result = mysqli_query($this->con,$query);
		return $result;
	}

	public function delCat($id) {
		$query = "DELETE FROM project_categories WHERE id=".$id;
		$result = mysqli_query($this->con,$query);
	}
}
?>